<?php
namespace Common\Model;

class GoodsModel extends CommonModel{
    protected $form_data = array();
    protected $goods_pics = array();
    protected $spec = array();
    protected $_validate = array(
        array('goods_name','require','请输入商品名称'),
        array('goods_pics','check_pic','请上传商品配图',1,'callback'),
        array('price','require','请输入商品价格'),
        array('spec','check_spec','请完善商品规格信息',0,'callback'),
        array('vip_price','check_vip','请完善VIP通道信息',0,'callback'),
//         array('vip_status','check_vip','请完善VIP通道价格信息',0,'callback'),
        array('per_first_price','check_per','请完善按天支付信息',0,'callback'),
        array('per_days','check_per_days','还款天数不是整数，请重新设置',0,'callback'),
//         array('per_price','check_per','',0,'callback'),
//         array('per_status','check_per','',0,'callback'),
        array('profile','require','请输入商品简介'),
        array('profile','3,60','商品简介限3~60个字符',0,'length'),
        array('detail','require','请输入商品详情'),
    );
    
    protected $_link = array(
    	'spec'=>array(
	   		'mapping_type'      => self::HAS_MANY,
	  		'class_name'        => 'GoodsSpec',
	  		'foreign_key'        => 'goods_id',
	    ),
    );
    
	public function _initialize(){
		$this->form_data = I('post.info');
		$this->goods_pics = I('post.goods_pics');
		if($this->form_data['type'] == '1'){
			$store = 0;
			foreach (I('post.spec') as $val){
				$this->spec[] = $val;
				$store += $val['store'];
			}
			$this->form_data['store'] = $store;
			$this->form_data['spec'] = $this->spec;
		}
		
		$this->goods_pics && $this->form_data['goods_logo'] = $this->goods_pics[0];
		$this->form_data['goods_pics'] = $this->goods_pics;
	}

    public function get_form(){
    	if(!$this->create($this->form_data))return false;
    	return $this->form_data;
    }
    
	protected function check_spec(){
		foreach ($this->spec as $val){
			if(empty($val['color']) || empty($val['store']))return false;
		}
		return true;
	}
	
	protected function check_pic(){
		if(is_array($this->goods_pics) && count($this->goods_pics) > 0)return true;
		return false;
	}
	
	protected function check_vip(){
		//if($this->form_data['vip_status'] == '1' && !$this->form_data['vip_price']) return false;
		if(!$this->form_data['vip_price']) return false;
		return true;
	}
	
	protected function check_per(){
		//if($this->form_data['per_status'] == '1' && (!$this->form_data['per_first_price'] || !$this->form_data['per_price'])) return false;
		if(!$this->form_data['per_first_price'] || !$this->form_data['per_price']) return false;
		return true;
	}
	
	protected function check_per_days(){
		if($this->form_data['per_days'] != floor($this->form_data['per_days'])) return false;
		return true;
	}
	public function integral($user_id){
	
		$goods = D('Goods')->where(array('id'=>I('post.goods_id'),'type'=>3))->find();
		if($goods['store']<1){
			$this->error='库存不足不能兑换';
			return false;
		}
		$user = D('User')->where(array('id'=>$user_id))->find();
		if($user['have_cent'] < $goods['integral_price']){
			$this->error='积分不足不能兑换';
			return false;
		}
		 
		//收货地址
		$address = D('Address')->where(array('id'=>I('post.address_id')))->find();
		//改变积分
		D('User')->where(array('id'=>$user['id']))->save(array('used_cent'=>$user['used_cent']+$goods['integral_price'],'have_cent'=>$user['have_cent']-$goods['integral_price']));
		//改变库存和兑换量
		D('Goods')->where(array('id'=>I('post.goods_id')))->save(array('store'=>--$goods['store'],'sale_count'=>++$goods['sale_count']));
		//积分记录
		$data['user_id'] = $user_id;
		$data['type'] = '2';
		$data['cent_value'] = $goods['integral_price'];
		$data['sense'] = $goods['goods_name'];
		if(!D('CentLog')->add($data)){
			$this->error='加入记录失败';
			return false;
		}
		//兑换订单
		$datas['user_id'] = $user_id;
		$datas['order_type'] = '3';
		$datas['order_sn'] = get_order_sn();
		$datas['order_integral_price'] = $goods['integral_price'];
		$datas['goods_id'] = $goods['id'];
		$datas['goods_name'] = $goods['goods_name'];
		$datas['goods_pic'] =  $goods['goods_pics'][0];
		$datas['goods_integral_price'] = $goods['integral_price'];
		$datas['address_id'] =  I('post.address_id');
		$datas['contact_user'] = $address['name'];
		$datas['contact_phone'] = $address['phone'];
		$datas['contact_area'] = $address['area'];
		$datas['contact_address'] = $address['address'];
		$datas['pay_time'] = time();
		$datas['status'] = 2;
		if(! $id = D('order')->add($datas)){
			$this->error='订单生成失败';
			return false;
		}
		$order['user_id'] = $user_id;
		$order['order_id'] = $id;
		$order['pay_type'] = 5;
		$order['pay_value'] = $goods['integral_price'];
		if(!D('PayLog')->add($order)){
			$this->error='插入记录失败';
			return false;
		}
		return true;
	}
	
}